package com.dkd.manage.service.impl;

import java.util.List;

import com.dkd.common.exception.ServiceException;
import com.dkd.common.utils.DateUtils;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.Vo.RegionVo;
import com.dkd.manage.mapper.EmpMapper;
import com.dkd.manage.mapper.NodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.RegionMapper;
import com.dkd.manage.domain.Region;
import com.dkd.manage.service.IRegionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 区域Service业务层处理
 * 
 * @author lcl
 * @date 2024-10-23
 */
@Service
public class RegionServiceImpl implements IRegionService 
{
    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询区域
     * 
     * @param id 区域主键
     * @return 区域
     */
    @Override
    public Region selectRegionById(Integer id)
    {
        return regionMapper.selectRegionById(id);
    }

    /**
     * 查询区域列表
     * 
     * @param region 区域
     * @return 区域
     */
    @Override
    public List<Region> selectRegionList(Region region)
    {
        return regionMapper.selectRegionList(region);
    }

    /**
     * 新增区域
     * 
     * @param region 区域
     * @return 结果
     */
    @Override
    public int insertRegion(Region region)
    {
        region.setCreateTime(DateUtils.getNowDate());
        int region_id = regionMapper.insertRegion(region);
        return region_id;
    }

    /**
     * 修改区域
     * 
     * @param region 区域
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRegion(Region region)
    {
        Integer rid = region.getId();

        // 获取region对象
        Region regionObj = regionMapper.selectRegionById(rid);
        region.setRegionName(regionObj.getRegionName());

        Integer updateRes = regionMapper.updateRegion(region);
        // 修改区域，同时要关联员工表里面的区域名称同步修改

        empMapper.updateEmpRegionNameByRegionId(rid,regionObj.getRegionName());
        return updateRes;
    }

    /**
     * 批量删除区域
     * 
     * @param ids 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteRegionByIds(Integer[] ids)
    {
        // 删除投放区域前查看是否关联了点位
        List<Node> nodeList = nodeMapper.selectNodeListByRegionId(ids);
        if (nodeList != null && nodeList.size() > 0){
            throw new ServiceException("该区域关联了投放点");
        }
        return regionMapper.deleteRegionByIds(ids);
    }

    /**
     * 删除区域信息
     * 
     * @param id 区域主键
     * @return 结果
     */
    @Override
    public int deleteRegionById(Integer id)
    {
        return regionMapper.deleteRegionById(id);
    }

    /**
     * 关联查询区域列表的节点数量
     * @param region
     * @return
     */
    @Override
    public List<RegionVo> selectRegionVoList(Region region) {
        return regionMapper.selectRegionVoList(region);
    }

    /**
     * 根据区域id获取节点列表
     * @param id
     * @return
     */
    @Override
    public List<Node> getNodeListByAreaId(Integer id) {
        return nodeMapper.getNodeListByAreaId(id);
    }
}
